package engine.sound
{
	import engine.asset.*;
	import engine.math.*;
	import flash.events.*;
	import flash.media.*;
	
	public class RxSoundChannel
	{
		private var m_channel:SoundChannel;
		private var m_transform:SoundTransform;
		private var m_callback:Function;
		private var m_volume:Number;
		private var m_attenuation:Number;
		private var m_pan:Number;
		private var m_loops:int;
		private var m_isMusicChannel:Boolean = false;
		private static var s_attenuationDistance:Number = 1000;
		private static var s_position:RxVector4;
		private static var s_playingSounds:Vector.<SoundChannel>;
		
		public function RxSoundChannel(isMusicChannel:Boolean = false)
		{
			this.m_isMusicChannel = isMusicChannel;
		}
		
		public static function Initialize() : void
		{
			s_position = new RxVector4(0, 0, 0, 0);
			s_playingSounds = new Vector.<SoundChannel>;
		}
		
		public static function SetAttenuationDistance(attenuationDistance:Number) : void
		{
			s_attenuationDistance = attenuationDistance;
		}
		
		public static function StopAllSounds() : void
		{
			var _loc_1:SoundChannel = null;
			for each (_loc_1 in s_playingSounds)
			{
				
				_loc_1.stop();
			}
			s_playingSounds.length = 0;
		}
		
		public function Play(param1:RxAssetSound, param2:Number = 1, param3:Number = 0, param4:Function = null, param5:int = 0) : void
		{
			this.m_volume = param2;
			this.m_pan = param3;
			this.m_callback = param4;
			this.m_loops = param5;
			this.m_attenuation = 0;
			return this._play(param1);
		}
		
		private function _play(param1:RxAssetSound) : void
		{
			var _loc_2:* = param1.GetSound();
			if (_loc_2)
			{
				this.m_transform = new SoundTransform(this.m_volume * (1 - this.m_attenuation), this.m_pan);
				this.m_channel = _loc_2.play(0, this.m_loops, this.m_transform);
				if (this.m_channel)
				{
					if (!this.m_isMusicChannel)
					{
						s_playingSounds.push(this.m_channel);
					}
					this.m_channel.addEventListener(Event.SOUND_COMPLETE, this.OnPlayComplete);
					return;
				}
			}
			this.m_transform = null;
			if (this.m_callback != null)
			{
				this.m_callback(this);
			}
		}
		
		private function OnPlayComplete(event:Event) : void
		{
			var _loc_2:* = s_playingSounds.indexOf(this.m_channel);
			if (_loc_2 != -1)
			{
				s_playingSounds.splice(_loc_2, 1);
			}
			this.m_channel.removeEventListener(Event.SOUND_COMPLETE, this.OnPlayComplete);
			if (this.m_callback != null)
			{
				this.m_callback(this);
			}
			this.m_channel = null;
			this.m_transform = null;
		}
		
		public function Stop() : void
		{
			if (this.m_channel)
			{
				this.m_channel.stop();
				this.m_channel = null;
				this.m_transform = null;
			}
		}
		
		public function SetVolume(param1:Number) : void
		{
			this.m_volume = param1;
			if (this.m_channel)
			{
				this.m_transform.volume = this.m_volume * (1 - this.m_attenuation);
				this.m_channel.soundTransform = this.m_transform;
			}
		}
	}
}